package com.xqq.lottery.protocal;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;
import org.xmlpull.v1.XmlSerializer;

import com.xqq.lottery.ConstantValue;
import com.xqq.lottery.NativeProtocal;

/**
 * ͷ���ķ�װ
 * @author qqixa
 *
 */
public class Header {
	
	private Leaf agenterid = new Leaf("agenterid",ConstantValue.AGENTERID);
    private Leaf source =new Leaf("source",ConstantValue.SOURCE);
    private Leaf compress= new Leaf("compress",ConstantValue.COMPRESS);
    private Leaf messengerid = new Leaf("messengerid");
    private Leaf timestamp =new Leaf("timestamp");
    private Leaf digest =new Leaf("digest");
    private Leaf transactiontype =new Leaf("transactiontype");
	private Leaf username =new Leaf("username");
	
	public void serializerHeader(XmlSerializer serializer,String body){
		//��timestamp��messengerid,Digest����value
		SimpleDateFormat format =new SimpleDateFormat("yyyyMMddHHmmss");
		String time = format.format(new Date());
		timestamp.setTagValue(time);
		//ʱ�������λ�����
		Random random =new Random();
		int num =  random.nextInt(999999)+1;
		DecimalFormat decimalFormat =new DecimalFormat("000000");
		messengerid.setTagValue(time+num+decimalFormat.format(num));
		//�����̵�����
		//digest + ʱ��� + ���������� + ����body�����ģ�
		//String digestValue = time + NativeProtocal.getAgenterPassword()+body;
		//digestValue = DigestUtils.md5Hex(digestValue);
		String digestValue = NativeProtocal.compressDigest(time, body);  //����native�������м���
		digest.setTagValue(digestValue);
		try{
			serializer.startTag(null, "header");
			agenterid.serializerLeaf(serializer);
			source.serializerLeaf(serializer);
			compress.serializerLeaf(serializer);
			messengerid.serializerLeaf(serializer);
			timestamp.serializerLeaf(serializer);
			digest.serializerLeaf(serializer);
			transactiontype.serializerLeaf(serializer);
			username.serializerLeaf(serializer);
			serializer.endTag(null, "header");
		}catch(Exception e){
			e.printStackTrace();
		}
	}

	public Leaf getTransactiontype() {
		return transactiontype;
	}

	public Leaf getUsername() {
		return username;
	}

	/**********************处理服务器端回复*************************/
	
	public Leaf getTimestamp() {
		return timestamp;
	}

	public Leaf getDigest() {
		return digest;
	}
	
	
}
